<?php
if ($ticket_parent!=1) {
	header('Location: class_manager.php');
	exit;
}

$result = mysql_query("select pkey from classes", $class_manager_db) or die(mysql_error($class_manager_db));
$num_classes = mysql_num_rows($result);
$per_page=10;
$max_page=(int)(($num_classes-1)/$per_page);
if($max_page<0)
	$max_page=0;

if(isset($_GET["page_nr"]) && intval($_GET["page_nr"])>0 && intval($_GET["page_nr"])<=$max_page){
	$page_nr = intval($_GET["page_nr"]);
}
else if(intval($_GET["page_nr"])>$max_page){
	$page_nr = $max_page;
}
else{
	$page_nr = 0;
}

$prev_page = $page_nr - 1;
if($prev_page < 0)
	$prev_page = 0;

$next_page = $page_nr + 1;
if($next_page > $max_page)
	$next_page = $max_page;
?>
<link rel="stylesheet" href="styles/datepicker.css" media="screen" />
<script type="text/javascript" src="scripts/datepicker.js"></script>

<script type="text/javascript">
	function expand_collapse_div(element_id){
		var t=document.getElementById(element_id);
		if(!t)return false;
		if(t.style.display=="none"){
			t.style.display="block";
		}
		else{
			t.style.display="none";
		}
		return true;
	}

	function delete_session(class_pkey, session_pkey){

		var delete_class_session_form = document.createElement("form");
		delete_class_session_form.setAttribute("method", "post");
		delete_class_session_form.setAttribute("action", "class_manager.php?page=admin&admin_page=edit_class&page_nr=<?php echo $page_nr;?>&updated="+ class_pkey +"#entry"+ class_pkey);

	    var delete_class_session_form_class_pkey = document.createElement("input");
	    delete_class_session_form_class_pkey.setAttribute("type","hidden");
	    delete_class_session_form_class_pkey.setAttribute("name","delete_class_session_form_class_pkey");
	    delete_class_session_form_class_pkey.setAttribute("value",class_pkey);
	    delete_class_session_form.appendChild(delete_class_session_form_class_pkey);

	    var delete_class_session_form_session_pkey = document.createElement("input");
	    delete_class_session_form_session_pkey.setAttribute("type","hidden");
	    delete_class_session_form_session_pkey.setAttribute("name","delete_class_session_form_session_pkey");
	    delete_class_session_form_session_pkey.setAttribute("value",session_pkey);
	    delete_class_session_form.appendChild(delete_class_session_form_session_pkey);

	    document.body.appendChild(delete_class_session_form);
	    delete_class_session_form.submit();

	    return true;
	}

	function popup_close(elementID) {
		document.getElementById(elementID).style.display = "none";

		return true;
	}
	
	function popup_open(elementID) {
		//var leftOffset = (document.width / 2) - 300;

		// Determine how much the visitor had scrolled

		var scrolledX = 0, scrolledY = 0;
		if( self.pageYOffset ) {
		  scrolledX = self.pageXOffset;
		  scrolledY = self.pageYOffset;
		} else if( document.documentElement && document.documentElement.scrollTop ) {
		  scrolledX = document.documentElement.scrollLeft;
		  scrolledY = document.documentElement.scrollTop;
		} else if( document.body ) {
		  scrolledX = document.body.scrollLeft;
		  scrolledY = document.body.scrollTop;
		}

		// Determine the coordinates of the center of the page

		var centerX = 0, centerY = 0;
		if( self.innerHeight ) {
		  centerX = self.innerWidth;
		  centerY = self.innerHeight;
		} else if( document.documentElement && document.documentElement.clientHeight ) {
		  centerX = document.documentElement.clientWidth;
		  centerY = document.documentElement.clientHeight;
		} else if( document.body ) {
		  centerX = document.body.clientWidth;
		  centerY = document.body.clientHeight;
		}

		var leftOffset = scrolledX + (centerX - 600) / 2;
		var topOffset = scrolledY + (centerY - 400) / 2;

		document.getElementById(elementID).style.top = topOffset + "px";
		document.getElementById(elementID).style.left = leftOffset + "px";
		document.getElementById(elementID).style.display = "block";

		return true;
	}

	function open_add_session(div_id, form_name, class_pkey){
		var add_class_session_form = document.getElementsByName(form_name)[0];
		add_class_session_form.setAttribute("action", "class_manager.php?page=admin&admin_page=edit_class&page_nr=<?php echo $page_nr;?>&updated="+class_pkey+"#entry"+class_pkey);
		add_class_session_form.elements["add_class_session_form_class_pkey"].setAttribute("value",class_pkey);

		
		popup_open(div_id);

		return true;
	}
</script>

<div id="add_class_session_popup" style="width:600px; display:none; position:absolute;">
	<table class="content" style="width: 600px">
		<tr>
			<td align="center" valign="middle"
				style="background-color: #1C2837; height: 23px; width: 576px; color: #ffffff">Add
				New Class Session</td>
			<td style="background-color: #1C2837"><a
				onclick="popup_close('add_class_session_popup')" class="clickable"><img
					src="images/close_button.png"
					style="height: 23px; width: 24px; border: 0;"> </a></td>
		</tr>
		<tr>
			<td colspan="2" style="width: 600px;">
				<form name="add_class_session_form" action="" method="post">
					<table class="form">
						<tr>
							<td align="right">Date (dd.mm.yyyy)*:</td>
							<td align="left"><input type="text" style="width: 100px"
								name="add_class_session_form_start_date">&nbsp; <a
								onclick="displayDatePicker('add_class_session_form_start_date',false,'dmy','.')"
								class="clickable"> <img src="images/cal.gif"
									alt="Pick a date..."
									style="width: 16px; height: 16px; border: 0px;">
							</a>
							</td>
							<td align="right">Time (HH:MM)*:</td>
							<td align="left"><input type="text" style="width: 100px"
								name="add_class_session_form_start_time">
							</td>
						</tr>
						<tr>
							<td align="right">Duration (hours)*:</td>
							<td align="left" colspan="3"><input type="text"
								style="width: 100px" name="add_class_session_form_duration">
							</td>
						</tr>
						<tr>
							<td valign="top" align="right">Comments:</td>
							<td align="left" colspan="3"><textarea rows="10"
									style="width: 450px" name="add_class_session_form_comments"></textarea>
							</td>
						</tr>
						<tr>
							<td colspan="4"><input type="hidden"
								name="add_class_session_form_class_pkey" value=""> <input
								type="submit" name="add_class_session_form_submit"
								value="Add Class Session" />
							</td>
						</tr>
					</table>
				</form>
			</td>
		</tr>
	</table>
</div>







<div class="present">
<h2 class="spaced">Edit Classes</h2>
<div class="bspaced">Click a class to expand it for editing.</div>
<?php 
$start_offset = $page_nr * $per_page;
$result = mysql_query("select ordered_classes.pkey, UNIX_TIMESTAMP(ordered_classes.reg_start) as reg_start, ordered_classes.capacity, ordered_classes.fee, ordered_classes.comments, ordered_classes.cancelled, UNIX_TIMESTAMP(ordered_classes.start_datetime) as start_datetime, modules.pkey as module, modules.short_name, modules.long_name from (select * from (select classes.pkey as pkey, classes.module, classes.reg_start, classes.capacity, classes.fee, classes.comments, classes.cancelled, sessions.start_datetime from classes left join sessions on classes.pkey=sessions.class order by start_datetime asc) class_w_startdate group by pkey order by start_datetime desc limit $start_offset, $per_page) ordered_classes left join modules on ordered_classes.module=modules.pkey", $class_manager_db) or die(mysql_error($class_manager_db));
?>
<table>
	<tr>
		<td style="text-align:left;">
			<a title="First Page" href="class_manager.php?page=admin&admin_page=edit_class&page_nr=0">first</a> <a title="Previous Page" href="class_manager.php?page=admin&admin_page=edit_class&page_nr=<?php echo $prev_page;?>">prev</a>
		</td>
		<td style="text-align:center;"></td>
		<td style="text-align:right;">
			<a title="Next Page" href="class_manager.php?page=admin&admin_page=edit_class&page_nr=<?php echo $next_page;?>">next</a> <a title="Last Page" href="class_manager.php?page=admin&admin_page=edit_class&page_nr=<?php echo $max_page;?>">last</a>
		</td>
	</tr>
<?php 
$i=0;
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
	
	if($line["start_datetime"] == NULL)
		$class_header=htmlspecialchars(stripslashes($line["long_name"]))." (???)";
	else
		$class_header=htmlspecialchars(stripslashes($line["long_name"]))." (".date("d M Y",$line["start_datetime"]).")";
?>
	<tr><td colspan="3"><hr></td></tr>
	<tr><td colspan="3">
		<a id="entry<?php echo $line["pkey"];?>"></a>
		<h3><a onclick="expand_collapse_div('class_details_<?php echo $i;?>')" class="clickable">
			<?php echo $class_header; if($line["cancelled"]==1) echo " - CANCELLED"?>
		</a></h3>
	</td></tr>
	<tr><td colspan="3">
		<div id="class_details_<?php echo $i;?>" <?php if(!isset($_GET["updated"]) || $_GET["updated"] != $line["pkey"]) echo 'style="display:none;"';?>>







			<form name="edit_class_form"
			action="class_manager.php?page=admin&admin_page=edit_class&page_nr=<?php echo $page_nr;?>&updated=<?php echo $line["pkey"];?>#entry<?php echo $line["pkey"];?>"
			method="post">
			<table class="form">
				<tr>
					<td align="left" colspan="4"><input type="checkbox" name="edit_class_form_cancelled" value="1" 
					<?php if($line["cancelled"] == 1) echo 'checked="yes"';?> />&nbsp;Class Cancelled</td>
				</tr>
				<tr>
					<td align="right">Module*:</td>
					<td align="left" colspan="3">
						<select style="width: 450px; margin: 0" name="edit_class_form_module">
							<?php
							$mod_result = mysql_query("SELECT pkey, long_name, short_name FROM modules ORDER BY long_name ASC", $class_manager_db) or die(mysql_error($class_manager_db));
							while ($mod_line = mysql_fetch_array($mod_result,MYSQL_ASSOC)) {
								echo '<option value="'. $mod_line["pkey"] .'"';
								if($line["module"]==$mod_line["pkey"])
									echo 'selected';
								echo '>'. htmlspecialchars(stripslashes($mod_line["long_name"])) .' ('.htmlspecialchars(stripslashes($mod_line["short_name"])).')</option>';
							}
							?>
						</select>
					</td>
				</tr>
				<tr>
					<td align="right">Fee*:</td>
					<td align="left"><input type="text" style="width: 100px"
						name="edit_class_form_fee" value="<?php echo $line["fee"];?>"></td>
					<td align="right">Capacity (leave blank for unlimited):</td>
					<td align="left"><input type="text" style="width: 100px"
						name="edit_class_form_capacity" <?php if($line["capacity"] != NULL) echo 'value="'.$line["capacity"].'"';?>></td>
				</tr>
				<tr>
					<td valign="top" align="right">Comments:</td>
					<td align="left" colspan="3"><textarea rows="10" style="width: 450px"
						name="edit_class_form_comments"><?php echo htmlspecialchars(stripslashes($line["comments"]));?></textarea></td>
				</tr>
				<tr>
					<td colspan="4" align="left"><h4>Registration Begin Date and Time</h4>&nbsp;&nbsp;&nbsp;(leave blank for none)</td>
				</tr>
				<?php 
				if($line["reg_start"]==NULL){
					$reg_start_date="";	
					$reg_start_time="";	
				}
				else {
					$reg_start_date=date("d.m.Y",$line["reg_start"]);
					$reg_start_time=date("H:i",$line["reg_start"]);
				}
				
				?>
				<tr>
					<td align="right">Date (dd.mm.yyyy):</td>
					<td align="left"><input type="text" style="width: 100px"
						name="edit_class_form_reg_start_date" value="<?php echo $reg_start_date;?>" id="reg_date_<?php echo $i;?>">&nbsp;
						<a onclick="displayDatePickerID('reg_date_<?php echo $i;?>',false,'dmy','.')" class="clickable"><img
							src="images/cal.gif" alt="Pick a date..."
							style="width: 16px; height: 16px; border: 0px;"> </a>
					</td>
					<td align="right">Time (HH:MM):</td>
					<td align="left"><input type="text" style="width: 100px"
						name="edit_class_form_reg_start_time" value="<?php echo $reg_start_time;?>">
					</td>
				</tr>
				<tr>
					<td valign="top" align="right"><h4>Class Sessions</h4><a onclick="open_add_session('add_class_session_popup','add_class_session_form','<?php echo $line["pkey"];?>')" class="clickable">add session</a></td>
					<td colspan="3">
					
					
					
					
					
					
					
					

							<table class="subform">
								<?php 
								$j=0;
								$session_result = mysql_query("SELECT pkey, UNIX_TIMESTAMP(start_datetime) as start_datetime, duration, comments 
									FROM sessions WHERE class=".$line["pkey"]." ORDER BY start_datetime ASC", $class_manager_db) or die(mysql_error($class_manager_db));
								while ($session_line = mysql_fetch_array($session_result, MYSQL_ASSOC)) {
								?>
									<tr>
										<td colspan="4"><hr></td>
									</tr>
									<tr>
										<td align="right">Date (dd.mm.yyyy)*:</td>
										<td align="left"><input type="text" style="width: 80px"
											name="edit_class_session_form_start_date_<?php echo $j;?>" 
											value="<?php echo date("d.m.Y",$session_line["start_datetime"]);?>" id="session_date_<?php echo $i;?>_<?php echo $j;?>">&nbsp;
											<a onclick="displayDatePickerID('session_date_<?php echo $i;?>_<?php echo $j;?>',false,'dmy','.')" class="clickable"><img
												src="images/cal.gif" alt="Pick a date..."
												style="width: 16px; height: 16px; border: 0px;"> </a>
										</td>
										<td align="right">Time (HH:MM)*:</td>
										<td align="left"><input type="text" style="width: 80px"
											name="edit_class_session_form_start_time_<?php echo $j;?>"
											value="<?php echo date("H:i",$session_line["start_datetime"]);?>">
										</td>
									</tr>
									<tr>
										<td align="right">Duration (hours)*:</td>
										<td align="left" colspan="3"><input type="text" style="width: 80px"
											name="edit_class_session_form_duration_<?php echo $j;?>"
											value="<?php echo $session_line["duration"];?>">
										</td>
									</tr>
									<tr>
										<td valign="top" align="right">Comments:</td>
										<td align="left" colspan="3">
											<textarea rows="10" style="width: 350px" name="edit_class_session_form_comments_<?php echo $j;?>"><?php echo htmlspecialchars(stripslashes($session_line["comments"]));?></textarea>
										</td>
									</tr>
									<tr>
										<td align="left" colspan="4">
											<a onclick="delete_session('<?php echo $line["pkey"];?>','<?php echo $session_line["pkey"];?>')" class="clickable">delete this session</a>
											<input type="hidden" name="edit_class_session_form_pkey_<?php echo $j;?>" value="<?php echo $session_line["pkey"];?>">
										</td>
									</tr>
								<?php
								$j++;
								}
								?>
									<tr>
										<td colspan="4"><hr></td>
									</tr>
							</table>
						
						
						
						
						
						
						
						
					</td>
				</tr>
				<tr>
					<td colspan="4" align="left">
						<input type="hidden" name="edit_class_form_pkey" value="<?php echo $line["pkey"];?>">
						<input type="submit" name="edit_class_form_submit" value="Submit Class Changes"/>
					</td>
				</tr>
			</table>
			</form>
			<b>Important:</b> Reducing the capacity will <b>not</b> automatically refigure registered and wait listed participants, this will have to be done manually.<br>
			* Required fields
				
				
				
				
				
				
				
		</div>
	</td></tr>
<?php
	$i++;
}
?>
	<tr><td colspan="3"><hr></td></tr>
	<tr>
		<td style="text-align:left;">
			<a title="First Page" href="class_manager.php?page=admin&admin_page=edit_class&page_nr=0">first</a> <a title="Previous Page" href="class_manager.php?page=admin&admin_page=edit_class&page_nr=<?php echo $prev_page;?>">prev</a>
		</td>
		<td style="text-align:center;"></td>
		<td style="text-align:right;">
			<a title="Next Page" href="class_manager.php?page=admin&admin_page=edit_class&page_nr=<?php echo $next_page;?>">next</a> <a title="Last Page" href="class_manager.php?page=admin&admin_page=edit_class&page_nr=<?php echo $max_page;?>">last</a>
		</td>
	</tr>
</table>
</div>